//
//  QSVisitMessageCell.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/30.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSVisitMessageCell.h"
#import "QSVisitProblemCell.h"
#import "QSProblemDetailViewController.h"
#import <SDAutoLayout.h>
//#import <SDWebImage/UIImageView+WebCache.h>
static NSString *defaultCell = @"DefaultCell";
static NSString *problemCell = @"ProblemCell";
@implementation QSVisitMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configSubView];
    [self configCollectinoView];
    [self configTableView];

}
- (void)configSubView{
    self.graybgView.layer.cornerRadius = 5;

    self.backgroundColor = [UIColor clearColor];
    self.contentView.layer.cornerRadius = 8;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self);
        make.trailing.equalTo(self);
        make.top.equalTo(self.mas_top).offset(kRealValue(0));
        make.bottom.equalTo(self.mas_bottom).offset(- kRealValue(10));
    }];
    self.userImageView.layer.cornerRadius = 22;
    self.userImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.userImageView.layer.masksToBounds = YES;
    self.openButton.selected = NO;
    self.openButton.backgroundColor = [UIColor clearColor];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)configCollectinoView{
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
        flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //左右间距
        flowLaout.minimumLineSpacing = 5.5;
    //上下间距
        flowLaout.minimumInteritemSpacing = 3;
    self.collectionView.collectionViewLayout = flowLaout;
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:defaultCell];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    [self setupAutoHeightWithBottomView:self.collectionView bottomMargin:10];

}

- (void)configTableView{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView registerNib:[UINib nibWithNibName:@"QSVisitProblemCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:problemCell];
}

- (void)setModel:(QSVisitDetailModel *)model{
    _model = model;
    [_userImageView sd_setImageWithURL:[NSURL URLWithString:_model.headPic]];
    _userNameLabel.text = _model.adminName;
    _dateLabel.text = [NSDate dateWithTimestamp:_model.evTime format:@"yyyy-MM-dd HH:mm"];
    _visitTypeLabel.text = _model.evType;
    _companyLabel.text = _model.companyName;
    _messageLabel.text = _model.evRemark;
    _contactLabel.text = [NSString stringWithFormat:@"跟进联系人: %@", _model.evFollow];
    _problemNumLabel.text = [NSString stringWithFormat:@"提交问题 %@ 条", _model.problemNums];
    
    CGFloat itemSize = kRealValue((kScreenWidth - kRealValue(51)) / 3);
    NSString *str = _model.evPic;
    if (str.length == 0) {
        self.collectionViewH.constant = itemSize * 0;
    }else{
    _picArr = [str componentsSeparatedByString:@","]; //字符串按照,分隔成数组
    if (_picArr.count > 0 && _picArr.count <= 3) {
        self.collectionViewH.constant = itemSize;
    }
    if (_picArr.count > 3 && _picArr.count <= 6) {
        self.collectionViewH.constant = itemSize * 2 + 5.5;
    }
    if (_picArr.count > 6 && _picArr.count <= 9) {
        self.collectionViewH.constant = itemSize * 3 + 11;
    }
    }
    [self.openButton addTarget:self action:@selector(openAction:) forControlEvents:UIControlEventTouchUpInside];
    if (_model.problemNums && _model.problemNums.integerValue > 0) {
        self.openButton.userInteractionEnabled = YES;
        [self.tableView reloadData];
    }else{
        self.tableViewH.constant = 0;
        self.openButton.userInteractionEnabled = NO;
    }
//    if (model.problemNums.integerValue > 0) {
//        [self setupAutoHeightWithBottomView:self.tableView bottomMargin:20];
//    }else{
//    }
    [self setupAutoHeightWithBottomView:self.tableView bottomMargin:20];
    self.problemArr = _model.problemList;
    [self.tableView reloadData];
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:defaultCell forIndexPath:indexPath];
    for(UIView *subView in cell.contentView.subviews){
        if(subView){
            [subView removeFromSuperview];
        }
    }
    UIImageView *pic = [[UIImageView alloc]initWithFrame:cell.contentView.frame];
    [pic sd_setImageWithURL:[NSURL URLWithString:_picArr[indexPath.item]]];
    pic.contentMode = UIViewContentModeScaleAspectFill;
    pic.layer.masksToBounds = YES;
    
    [cell.contentView addSubview:pic];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSource = self;
        browser.currentPage = indexPath.item;
        //展示
        [browser show];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenWidth - kRealValue(51)) / 3, (kScreenWidth - kRealValue(51)) / 3);
}

//内边距处理
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // UIEdgeInsets insets = {top, left, bottom, right};
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - TableViewDelegate % DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.problemArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QSVisitProblemCell *cell = [tableView dequeueReusableCellWithIdentifier:problemCell];
//    if (!cell) {
//        cell = [tableView dequeueReusableCellWithIdentifier:problemCell];
//    }
    cell.model = self.problemArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kRealValue(84);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    QSVisitProblemModel *model = self.problemArr[indexPath.row];
//    QSProblemDetailViewController *vc = [QSProblemDetailViewController new];
//    vc.problemId = model.id;
//    [[UIViewController currentViewController].navigationController pushViewController:vc animated:YES];
}

- (void)openAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        DLog(@"展开");
        [self checkProblemNums];
        [self setupAutoHeightWithBottomView:self.tableView bottomMargin:20];
        [sender setTitle:@"收起" forState:UIControlStateNormal];
    }else{
        DLog(@"收起");
        self.tableViewH.constant = 0;
        [self setupAutoHeightWithBottomView:self.graybgView bottomMargin:20];
        [sender setTitle:@"展开" forState:UIControlStateNormal];
    }
    _model.tableH = self.tableViewH.constant;
    if (self.didOpenTheProblemListBlock) {
        self.didOpenTheProblemListBlock(sender, self.tableViewH.constant);
    }
}

- (void)checkProblemNums{
    if (_model.problemNums &&_model.problemNums.integerValue > 0) {
           self.tableViewH.constant = kRealValue(kRealValue(84) * _model.problemNums.floatValue);
       }else{
           self.tableViewH.constant = 0;
       }
}

#pragma YBimageBowerDelegate

- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser{
    return _picArr.count;
}

- (id<YBIBDataProtocol>)yb_imageBrowser:(YBImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index{
     YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:_picArr[index]];
    //    data.sourceObject = _pictureList[index].;
        return data;
}

// 实现 <YBImageBrowserDelegate>

@end
